** This file creates Figures 5.1 and 5.2

* Open dataset from NIMSP on campaign contributions 
use nimsp_cleaned, clear

* Collapse data to create dataset of total campaign contributions in each state/election
collapse (sum) totcont, by(statabbr elecyear)

* Rename variables and save dataset to be brought back in later
ren statabbr state
ren elecyear year
sort state year
save nimsp_collapsed, replace

* Open data from NIMSP on independent spending
use targetdata, clear

* Sort data and merge with a list of groups coded for partisan affiliation
sort spendername
merge spendername using coded_groups
drop _m

* Keep states in which coverage of independent spending is relatively comprehensive
keep if statecode=="AK" | statecode=="AZ" | statecode=="CA" | statecode=="CO" | statecode=="CT" | statecode=="FL" | statecode=="ID" | statecode=="IA" | statecode=="ME" | statecode=="MA" | statecode=="MI" | statecode=="MN" | statecode=="MO" | statecode=="NC" | statecode=="OH" | statecode=="OK" | statecode=="TN" | statecode=="TX" | statecode=="WA" | statecode=="WI"

* Keep spending on behalf of state legislative candidates only
keep if targetedoffice=="HOUSE" | targetedoffice=="SENATE" | targetedoffice=="ASSEMBLY"

* Create variables for different categories of independent spending based on source
gen dem_ie=targetamount if spenderparty=="DEMOCRAT" | spenderparty=="DEMOCRATIC-FARMER-LABOR"
replace dem_ie=targetamount if shadowparty==1 & shadowpartykind=="D"
gen rep_ie=targetamount if spenderparty=="REPUBLICAN"
replace rep_ie=targetamount if shadowparty==1 & shadowpartykind=="R"
gen other_ie=targetamount if other==1 | lawyer==1 | candidate==1
gen union_ie=targetamount if union==1
gen business_ie=targetamount if business==1
gen issue_ie=targetamount if issue==1
gen shadowparty_ie=targetamount if shadowparty==1

* Collapse to create dataset of total given in each category
collapse (sum) dem_ie rep_ie other_ie union_ie business_ie issue_ie shadowparty_ie targetamount, by( statecode  yearcode)

* Drop state/year observations where independent spending was minimal (less than $100k total)
drop if targetamount==0 | targetamount==.
drop if targetamount<100000

* Rename variables, sort data, and then merge in dataset on camp finance laws
ren statecode state
ren yearcode year
sort state year
merge state year using masterdataset2
drop if _m<3
drop _m

* Sort data and merge in dataset on contribution data
sort state year
merge state year using nimsp_collapsed
drop if _m<3

* Create variables that are the ratio of independent spending to total contributions
gen ie_ratio=targetamount/totcont
gen partyie_ratio=(dem_ie+rep_ie)/totcont
gen otherie_ratio=(other_ie)/totcont
gen demie_ratio=(dem_ie)/totcont
gen repie_ratio=(rep_ie)/totcont
gen unionie_ratio=(union_ie)/totcont
gen businessie_ratio=business_ie/totcont
gen issueie_ratio=issue_ie/totcont
gen shadowpartyie_ratio=shadowparty_ie/totcont

* Generate variable for whether parties are limited
gen bothunlimited=1 if l_party_cand==0 & l_ind_party==0
replace bothunlimited=0 if l_party_cand==1 | l_ind_party==1
label define partylimit 0 "Limits on Parties" 1 "No Limits on Parties"
label values bothunlimited partylimit

* Create Figure 5.2
graph bar demie_ratio repie_ratio unionie_ratio businessie_ratio issueie_ratio, legend(lab(1 "Democratic IE") lab(2 "Republican IE") lab(3 "Union IE") lab(4 "Business IE") lab(5 "Issue Group IE") col(3)) bar(1, col(gs0) fi(100)) bar(2, col(gs0) fi(67)) bar(3, col(gs0) fi(0)) bar(4, col(gs0) fi(33)) bar(5, col(gs0) fi(0) lc(black) lp(dash)) ytitle("Ind. Expend. Recorded per $1 of Direct Contributions") ylabel(,nogrid) by(bothunlimited,  note("")) scheme(s1mono)
graph export fig_5_2.eps, replace

* Generate numeric code for state variable
encode state, gen(st)

* Generate total of IEs by categorized groups
gen totalie=demie_ratio +repie_ratio +unionie_ratio+ businessie_ratio +issueie_ratio

* Generate bar graphs for each class of states
graph bar totalie if year>=2010 & l_party_cand==0 & l_ind_party==0, over(st) hor stack legend(lab(1 "Democratic IE") lab(2 "Republican IE") lab(3 "Union IE") lab(4 "Business IE") lab(5 "Issue Group IE")) bar(1, col(black)) ytitle("Ind. Expend. Recorded per $1" "of Direct Contributions", size(medium)) ylabel(,nogrid) scheme(s1color) title("States with No Limits" "on Parties", size(vlarge)) saving(nolimits.gph, replace)
graph bar totalie if year>=2010 & l_party_cand==1 & l_ind_party==0, over(st) hor stack legend(lab(1 "Democratic IE") lab(2 "Republican IE") lab(3 "Union IE") lab(4 "Business IE") lab(5 "Issue Group IE")) bar(1, col(black))  ytitle("Ind. Expend. Recorded per $1" "of Direct Contributions", size(medium)) ylabel(,nogrid) scheme(s1color) title("States Limiting" "Contributions from Parties", size(vlarge)) saving(fromlimits.gph, replace)
graph bar totalie if year>=2010 & l_party_cand==0 & l_ind_party==1, over(st) hor stack legend(lab(1 "Democratic IE") lab(2 "Republican IE") lab(3 "Union IE") lab(4 "Business IE") lab(5 "Issue Group IE")) bar(1, col(black))  ytitle("Ind. Expend. Recorded per $1" "of Direct Contributions", size(medium)) ylabel(,nogrid) scheme(s1color) title("States Limiting" "Contributions to Parties", size(vlarge)) saving(tolimits.gph, replace)
graph bar totalie if year>=2010 & l_party_cand==1 & l_ind_party==1, over(st) hor stack legend(lab(1 "Democratic IE") lab(2 "Republican IE") lab(3 "Union IE") lab(4 "Business IE") lab(5 "Issue Group IE")) bar(1, col(black))  ytitle("Ind. Expend. Recorded per $1" "of Direct Contributions", size(medium)) ylabel(,nogrid) scheme(s1color) title("States Limiting" "Contributions to/from Parties", size(vlarge)) saving(bothlimits.gph, replace)

* Combine into one graph for Figure 5.1
graph combine nolimits.gph tolimits.gph fromlimits.gph bothlimits.gph, graphr(c(white)) xcom
graph export fig_5_1.eps, replace

